# -*- coding: utf-8 -*-
import xlsxwriter

def get_format(wd, option={}):
    return wd.add_format(option)

# 设置居中
def get_format_center(wd,num=1):
    return wd.add_format({'align': 'center','valign': 'vcenter','border':num})
def set_border_(wd, num=1):
    return wd.add_format({}).set_border(num)

# 写数据
def _write_center(worksheet, cl, data, wd):
    return worksheet.write(cl, data, get_format_center(wd))

def init(workbook,worksheet,reportSummary):

    # 设置列行的宽高
    worksheet.set_column("A:A", 15)
    worksheet.set_column("B:B", 20)
    worksheet.set_column("C:C", 20)
    worksheet.set_column("D:D", 20)
    worksheet.set_column("E:E", 20)
    worksheet.set_column("F:F", 20)

    worksheet.set_row(1, 30)
    worksheet.set_row(2, 30)
    worksheet.set_row(3, 30)
    worksheet.set_row(4, 30)
    worksheet.set_row(5, 30)

    # worksheet.set_row(0, 200)
    define_format_H1 = get_format(workbook, {'bold': True, 'font_size': 18})
    define_format_H2 = get_format(workbook, {'bold': True, 'font_size': 14})
    define_format_H1.set_border(1)

    define_format_H2.set_border(1)
    define_format_H1.set_align("center")
    define_format_H2.set_align("center")
    define_format_H2.set_bg_color("blue")
    define_format_H2.set_color("#ffffff")

    # Create a new Chart object.
    worksheet.merge_range('A1:F1', u'测试报告总概况', define_format_H1)
    worksheet.merge_range('A2:F2', u'测试概括', define_format_H2)
    worksheet.merge_range('A3:A6', u'报表信息', get_format_center(workbook))

    _write_center(worksheet, "B3", u'项目名称', workbook)
    _write_center(worksheet, "B4", u'接口版本', workbook)
    _write_center(worksheet, "B5", u'脚本语言', workbook)
    _write_center(worksheet, "B6", u'测试网络', workbook)

    _write_center(worksheet, "C3", reportSummary['test_name'], workbook)
    _write_center(worksheet, "C4", reportSummary['test_version'], workbook)
    _write_center(worksheet, "C5", reportSummary['test_pl'], workbook)
    _write_center(worksheet, "C6", reportSummary['test_net'], workbook)

    _write_center(worksheet, "D3", u"接口总数", workbook)
    _write_center(worksheet, "D4", u"通过总数", workbook)
    _write_center(worksheet, "D5", u"失败总数", workbook)
    _write_center(worksheet, "D6", u"测试日期", workbook)

    _write_center(worksheet, "E3", reportSummary['test_sum'], workbook)
    _write_center(worksheet, "E4", reportSummary['test_success'], workbook)
    _write_center(worksheet, "E5", reportSummary['test_failed'], workbook)
    _write_center(worksheet, "E6", reportSummary['test_date'], workbook)

    _write_center(worksheet, "F3", u"分数", workbook)

    worksheet.merge_range('F4:F6', reportSummary['test_score'], get_format_center(workbook))

    pie(workbook, worksheet)

 # 生成饼形图
def pie(workbook, worksheet):
    chart1 = workbook.add_chart({'type': 'pie'})
    chart1.add_series({
    'name':       u'接口测试统计',
    'categories':u'=测试总况!$D$4:$D$5',
   'values':    u'=测试总况!$E$4:$E$5',
    })
    chart1.set_title({'name': u'接口测试统计'})
    chart1.set_style(10)
    worksheet.insert_chart('A9', chart1, {'x_offset': 25, 'y_offset': 10})

def test_detail(workbook,worksheet,reportDetails):

    # 设置列行的宽高
    worksheet.set_column("A:A", 7.5)
    worksheet.set_column("B:B", 20)
    worksheet.set_column("C:C", 12.5)
    worksheet.set_column("D:D", 50)
    worksheet.set_column("E:E", 60)
    worksheet.set_column("F:F", 20)
    worksheet.set_column("G:G", 20)
    worksheet.set_column("H:H", 20)

    #worksheet.set_row(1, 30)
    #worksheet.set_row(2, 30)
    #worksheet.set_row(3, 30)
    #worksheet.set_row(4, 30)
    #worksheet.set_row(5, 30)
    #worksheet.set_row(6, 30)
    #worksheet.set_row(7, 30)

    worksheet.merge_range('A1:H1', u'测试详情', get_format(workbook, {'bold': True, 'font_size': 18 ,'align': 'center','valign': 'vcenter','bg_color': 'blue', 'font_color': '#ffffff'}))
    _write_center(worksheet, "A2", u'用例ID', workbook)
    _write_center(worksheet, "B2", u'接口名称', workbook)
    _write_center(worksheet, "C2", u'接口协议', workbook)
    _write_center(worksheet, "D2", u'URL', workbook)
    _write_center(worksheet, "E2", u'参数', workbook)
    _write_center(worksheet, "F2", u'预期值', workbook)
    _write_center(worksheet, "G2", u'实际值', workbook)
    _write_center(worksheet, "H2", u'测试结果', workbook)

    green = workbook.add_format({'border': 1, 'align': 'center', 'valign': 'vcenter', 'bg_color': '#cce8cf', 'font_size': 12})
    blue = workbook.add_format({'border': 1, 'align': 'center', 'valign': 'vcenter', 'bg_color': '#5e9cd3', 'font_size': 12})

    row = 0
    for index1 in range(len(reportDetails)):
        for index2 in range(len(reportDetails[index1])):
            row+=1
            worksheet.set_row(row, 30)
            if index1 % 2 == 0:
                worksheet.write(index1 + 2, index2, reportDetails[index1][index2], green)
            else:
                worksheet.write(index1 + 2, index2, reportDetails[index1][index2], blue)

def outputReport(reportSummary,reportDetails):
    workbook = xlsxwriter.Workbook(u'output/测试报告.xlsx')
    worksheet = workbook.add_worksheet(u"测试总况")
    worksheet2 = workbook.add_worksheet(u"测试详情")

    init(workbook, worksheet, reportSummary)
    test_detail(workbook, worksheet2, reportDetails)

    workbook.close()